package com.gtm.config;

import com.gtm.model.User;
import com.gtm.repository.UserRepository;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static com.gtm.config.SHA1.encode;

public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserRepository usr;



    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authcToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        String name = token.getUsername();
        String password = String.valueOf(token.getPassword());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("username", name);
        //密码进行加密处理  明文为  password+name
        SHA1 sha1=new SHA1();
        String passwordSHA1=encode(password);
        map.put("password", passwordSHA1);
        User user=usr.findByUsernameAndPasswd(name,passwordSHA1);
        if (null == user) {
            throw new AccountException("帐号或密码不正确！");
        }
        //System.out.println(user.getRealname());
        return new SimpleAuthenticationInfo(user, password, getName());
    }

    /**

     * 授权

     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals) {
        System.out.println("权限认证方法：MyShiroRealm.doGetAuthorizationInfo()");
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        SimpleAuthorizationInfo info =  new SimpleAuthorizationInfo();
        //根据用户ID查询角色（role），放入到Authorization里。

		/*Map<String, Object> map = new HashMap<String, Object>();

		map.put("user_id", userId);

		List<SysRole> roleList = sysRoleService.selectByMap(map);

		Set<String> roleSet = new HashSet<String>();

		for(SysRole role : roleList){

			roleSet.add(role.getType());

		}*/
        Set<String> roleSet = new HashSet<String>();
        String role="";
        switch (user.getType()){
            case 0:
                role="student";
                break;
            case 1:
                role="teacher";
                break;
            case 2:
                role="director";
                break;
            case 3:
                role="office";
                break;
        }
        roleSet.add(role);
        info.setRoles(roleSet);
        //根据用户ID查询权限（permission），放入到Authorization里。

		/*List<SysPermission> permissionList = sysPermissionService.selectByMap(map);

		Set<String> permissionSet = new HashSet<String>();

		for(SysPermission Permission : permissionList){

			permissionSet.add(Permission.getName());

		}*/
        Set<String> permissionSet = new HashSet<String>();
//        permissionSet.add("权限添加");
//        permissionSet.add("权限删除");
        String perm="";
        switch (user.getType()){
            case 0:
                perm="student";
                break;
            case 1:
                perm="teacher";
                break;
            case 2:
                perm="director";
                break;
            case 3:
                perm="office";
                break;
        }
        permissionSet.add(perm);
        info.setStringPermissions(permissionSet);
        return info;
    }
}